<?php
//获取文件信息
function sys_config($name,$dir=''){
	static $cmsconfig=array();
	if(isset($cmsconfig[$name])){
		return $cmsconfig[$name];
	}
	if($dir==''){$dir=APPS_PAHT."/data/config/";}
	$file=$dir.$name.'.php';
	if(!is_file($file)){
		msg_error($file.'文件不存在');
	}
   	$cmsconfig[$name]=require($file);
    return $cmsconfig[$name];
}
//更改配置文件信息 php数组$data 写入文件的数组
function sys_upconfig($name,$data=array(),$dir=''){
	if(empty($data)){
		return false;
	}
	//写入配置文件数组
	if($dir==''){$dir=APPS_PAHT."/data/config/";}
	$file=$dir.$name.".php";
	if(!is_file($file)){
		msg_error($file.'文件不存在');
	}
	$return='<?php return '.var_export($data,true)." ?>";
	return file_put_contents($file,$return);
}
function msg_error($str){
    echo '<meta charset="utf-8">';
    trigger_error($str, E_USER_ERROR);exit;
}
//公共ajaxs返回
function ajax_return($status,$message,$data=null){
	$ajax_return['message']=$message;
	$ajax_return['status']=$status;
	$ajax_return['data']=$data;
	echo json_encode($ajax_return);exit;
}
/**
* 正则验证 
* @return
*/
function check_regex($value,$rule){
    $validate = array(
		'istell'    =>  '/^([0-9]{3,4}-)?[0-9]{7,8}$/',//tell 验证
		'ismobile'  =>  '/^1[3|4|5|7|8][0-9]\d{8}$/',//手机
        'email'     =>  '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',//邮箱
        'url'       =>  '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
        'http'		=>  '/^http(s?):\/\//',
        'number'    =>  '/^\d+$/',
        'integer'   =>  '/^[-\+]?\d+$/',
        'double'    =>  '/^[-\+]?\d+(\.\d+)?$/',
        'english'   =>  '/^[A-Za-z]+$/',
        'englishh'   =>  '/^[A-Za-z|]+$/',//只能是字母和|
        'englishx'  =>  '/^[A-Za-z_]+$/',//字母加下划线
        'isnozh'    =>	'/^[a-zA-Z0-9]+$/',
        '_isnozh'    =>	'/^[a-zA-Z0-9_]+$/',
        'filename'	=> 	'/^[A-Za-z0-9_.]+$/',//文件名验证
        'dir'      	=>	'/^[a-zA-Z0-9_\/]+$/',//目录结果
        'card'		=>  '/^(\d{18,18}|\d{15,15}|\d{17,17}x)$/',
        'price'     =>  "/^\d+\.?\d{0,2}$/",
		'zint'			=>'/^\d+$/',
        'iszhen'    =>  '/[\x7f-\xff]/',//存在中文 no 不存在中文
        'iszhen2'   =>  '/^[\x{4e00}-\x{9fa5}]+$/u',//全是中文  no 不全是中文
        'comstrunderline'    =>  '/^[0-9a-zA-Z_\x{4e00}-\x{9fa5}]+$/iu',//中文 英文 数字 下划线
		'title'    =>  '/^[0-9a-zA-Z\?\。\x{4e00}-\x{9fa5}]+$/iu',//中文 英文 数字 下划线 ? .
        'comstr'    =>  '/^[0-9a-zA-Z\x{4e00}-\x{9fa5}]+$/iu',//中文 英文 数字
		'width'	=>'/^(\d+)|(\d+%)$/',//宽带+百分比
		'empty' =>'/^[\\s]*$/',//是否为空值
    );
    // 检查是否有内置的正则表达式
    if(isset($validate[strtolower($rule)]))
        $rule = $validate[strtolower($rule)];
    return preg_match($rule,$value)===1;
}
/**
*用户密码加密方式
*/
function userpassmd5($password){
    return md5('pms'.sha1(md5('hcpms'.$password.'2020')));
}
//唯一标识
function getuniqidstring($id) {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $uuid =substr($charid, 0, 8)
    .substr($charid, 8, 4)
    .substr($charid,12, 4)
    .substr($charid,16, 4)
    .substr($charid,20,12);
    return md5($uuid.$id);
}
//获取ip地址
function get_client_ip($type = 0) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if(isset($_SERVER['HTTP_X_REAL_IP'])){//nginx 代理模式下，获取客户端真实IP
        $ip=$_SERVER['HTTP_X_REAL_IP'];
    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
        $ip     =   $_SERVER['HTTP_CLIENT_IP'];
    }elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
        $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos    =   array_search('unknown',$arr);
        if(false !== $pos) unset($arr[$pos]);
        $ip     =   trim($arr[0]);
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
    }else{
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}
//写入操作日志
function useraddrecord($type,$des){
    $userinfo=unserialize($_SESSION['pmsuserinfo']);
    $data['type']=$type;
    $data['uid']=$userinfo['id'];
    $data['recordtime']=time();
    $data['remark']=$des;
    $ci =& get_instance();
    $ci->load->database();
    $ci->db->insert('pm_user_record',$data);
    $userrecordid=$ci->db->insert_id('id');
    return $userrecordid>0?true:false;
}
?>